<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入用户" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="货币" prop="moneyId">
        <el-select v-model="queryParams.moneyId" placeholder="请选择" clearable @change="handleQuery">
          <el-option v-for="item in moneyList" :key="item.id" :label="item.moneyName" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="来源" prop="source">
        <el-select v-model="queryParams.source" placeholder="请选择" clearable @change="handleQuery">
          <el-option v-for="item in souceList" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['money:moneyRechargeRecord:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="moneyRechargeRecordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户" align="center" prop="userInfo.userName" />
      <el-table-column label="货币" align="center" prop="moneyName" />
      <el-table-column label="充值前余额" align="center" prop="beforeBalance" />
      <el-table-column label="充值数量" align="center" prop="amount" />
      <el-table-column label="充值后余额" align="center" prop="afterBalance" />
      <el-table-column label="充值备注" align="center" prop="remark" />
      <el-table-column label="来源" align="center">
        <template #default="scope">
          {{ scope.row.source == 0 ? '后台' : '前台' }}
        </template>
      </el-table-column>
      <el-table-column label="支付凭证" align="center">
        <template #default="scope">
          <ImagePreview :src="scope.row.voucher" width="100px" height="100px" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="审核信息" align="center">
        <template #default="scope">
          <p>审核状态:{{ scope.row.verifyState == 0 ? '未审核' : scope.row.verifyState == 1 ? '通过' : '不通过' }}</p>
          <p>审核备注:{{ scope.row.verifyRemark }}</p>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleExamine(scope.row)"
            v-if="scope.row.source == 1">审核</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <el-dialog title="充值审核" v-model="examineShow" width="500px" append-to-body>
      <el-form ref="examineFormRef" :model="examineForm" label-width="80px">
        <el-form-item label="审核状态" prop="verifyState">
          <el-radio-group v-model="examineForm.verifyState">
            <el-radio :label="1">通过</el-radio>
            <el-radio :label="2">不通过</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核备注" prop="verifyRemark">
          <el-input v-model="examineForm.verifyRemark" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitExamineForm">确 定</el-button>
          <el-button @click="examineShow = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="MoneyRechargeRecord">
import { listMoneyRechargeRecord, auditMoneyRechargeRecord } from "@/api/money/moneyRechargeRecord";
import { listMoney } from "@/api/money/money";

const { proxy } = getCurrentInstance();

const moneyRechargeRecordList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userName: null,
    moneyId: null,
    beforeBalance: null,
    amount: null,
    afterBalance: null,
    source: null
  },
});

const { queryParams } = toRefs(data);

/**
 * 订单审核
 */
const examineShow = ref(false);
const examineForm = ref({});
// 审核处理
function handleExamine(row) {
  examineForm.value = {
    id: row.id,
    verifyState: null,
    verifyRemark: null
  };
  examineShow.value = true;
}

// 提交审核
function submitExamineForm() {
  proxy.$modal.confirm("确认数据并审核?").then(function () {
    auditMoneyRechargeRecord(examineForm.value).then((res) => {
      getList();
      examineShow.value = false;
      proxy.$modal.msgSuccess("审核成功");
    });
  });
}


const souceList = ref([
  {
    label: '后台',
    value: 0
  },
  {
    label: '前台',
    value: 1
  }
])

const moneyList = ref([])

function getMoneyList() {
  listMoney({
    pageNum: 1,
    pageSize: 9999
  }).then(res => {
    moneyList.value = res.rows || []
  })
}

getMoneyList()

/** 查询货币充值记录列表 */
function getList() {
  loading.value = true;
  listMoneyRechargeRecord(queryParams.value).then(response => {
    moneyRechargeRecordList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}


/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}



/** 导出按钮操作 */
function handleExport() {
  proxy.download('money/moneyRechargeRecord/export', {
    ...queryParams.value
  }, `moneyRechargeRecord_${new Date().getTime()}.xlsx`)
}

getList();
</script>
